﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Inovout.Gui.Wpf.ViewModel;
using System.Windows;
using Inovout.Business;

namespace Inovout.Gui.Wpf
{
    public static class WorkbenchInstance
    {
        internal static IWorkbench MainWorkbench;

        public static void Dialog(WorkItemContext workItemContext)
        {
            MainWorkbench.DialogView(CreateViewContent(workItemContext));
        }
        public static void View(WorkItemContext workItemContext)
        {
            MainWorkbench.ShowView(CreateViewContent(workItemContext));
          //  MainWorkbench.ReBuildToolBar(workItemContext.Functions);
        }

        private static IViewContent CreateViewContent(WorkItemContext workItemContext)
        {
            IViewContent viewContent = ComponentManager.Resolve<IViewContent>(workItemContext.ViewName.ToLower() + "view");
            Type modelType =workItemContext.ViewData.Model.GetType();
            string viewModelKey = modelType.Name;
            if(modelType.IsGenericType)
            {
                modelType = modelType.GetGenericArguments()[0];
                viewModelKey = modelType.Name + "collection";
            }
            BaseViewModel viewModel = ComponentManager.Resolve<BaseViewModel>(viewModelKey.ToLower() + "viewmodel");
            viewModel.ViewData = workItemContext.ViewData;
            ((BaseView)viewContent).DataContext = viewModel;
            return viewContent;
        }

        public static void Message(string message)
        {
            MessageBox.Show(message);

        }
    }
}

